<?

include "../config/constants.php";

if( !$_GET[TEST] )
{
	if( !$_POST[CID] )
	{
		echo "ERROR";
		exit;
	}
	$cid = $_POST[CID];
}else
	$cid = $_GET[CID];

$mysqli = mysqli_connect("localhost", "canss", "1022", "contentsmetadata_db") or die("Could not connect: ".mysql_error());
mysqli_query($mysqli, "SET NAMES 'utf8'");
$wsclient = new SoapClient(WS_PORTAL);


$query1 = "
	DELETE FROM `contentsmetadata_db`.`tblLocation`
	WHERE `tblContent_tblCID_cid`='$cid';";
$query2 = "
	DELETE FROM `contentsmetadata_db`.`tblContent`
	WHERE `tblCID_cid`='$cid';";
if( $query1 && mysqli_query( $mysqli, $query1 )!= 1 )
{echo "ERROR: Fail to delete from MDB".$query1;}
else if( $query2 && mysqli_query( $mysqli, $query2 )!= 1 )
{echo "ERROR: Fail to delete from MDB2".$query2;}



$query3 = "
	DELETE FROM `contentsmetadata_db`.`tblCID`
	WHERE `cid`='$cid';";
$query4 = "
	DELETE FROM `contentsmetadata_db`.`tblContentDescription`
	WHERE `tblCID_cid`='$cid';";
$query5 = "
	DELETE FROM `contentsmetadata_db`.`tblLinkage`
	WHERE `tblCID_cid`='$cid';";
if( $query3 && mysqli_query( $mysqli, $query3 )!= 1 )
{echo "ERROR: Fail to delete from MDB3".$query3;}
if( $query4 && mysqli_query( $mysqli, $query4 )!= 1 )
{echo "ERROR: Fail to delete from MDB4".$query4;}
if( $query5 && mysqli_query( $mysqli, $query5 )!= 1 )
{echo "ERROR: Fail to delete from MDB5".$query5;}


exec("rm /var/www/MediaSource/khj/$cid.* -f");
if( $_POST[DELCONTENT] )
{
	exec("rm /var/www/MediaFarm/Content/$cid.* -f");
}


/*
$response2 = $wsclient->deleteCID( array('_cid'=>$cid) )->return;
if( !$response2 )
{
	echo "ERROR: deleteCID return false value for $cid<br/>";
}
*/

if( $_POST[_rurl] )
{
	$rurl = $_POST[_rurl];
	header( "Location: http://portal.canss.net/$rurl" );
}

?>
